import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/models/response/my_comment_response.dart';
import 'package:sainbainu/common/routes/routes.dart';
import 'package:sainbainu/common/values/colors.dart';
import 'package:sainbainu/common/widgets/widget.dart';

import 'index.dart';

class CommentForwardPage extends StatelessWidget {
  const CommentForwardPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    /// 这个必须在这里初始化 否则无法保持状态
    var controller = Get.put(CommentForwardController());

    return LoginWrapper(
      child: EasyRefresh.builder(
        controller: controller.state.refreshController,
        onLoad: controller.onRichBottom,
        childBuilder: (context, physics) {
          return CustomScrollView(
            physics: physics,
            slivers: [
              _buildListView(),
            ],
          );
        },
      ),
    );
  }

  /// 列表
  Widget _buildListView() {
    return GetBuilder<CommentForwardController>(
      id: 'contentList',
      builder: (_) {
        return SliverList.separated(
          itemCount: _.state.contentList.rows.length,
          separatorBuilder: (context, index) {
            return const SizedBox(height: 10);
          },
          itemBuilder: (context, index) {
            MyCommentResponse item = _.state.contentList.rows[index];

            return _buildListItem(_, item);
          },
        );
      },
    );
  }

  Widget _buildListItem(
    CommentForwardController controller,
    MyCommentResponse item,
  ) {
    return GestureDetector(
      onTap: () => controller.toContentDetailPage(item),
      child: Container(
        color: Colors.white,
        padding: const EdgeInsets.only(
          top: 20,
          left: 20,
          right: 20,
          bottom: 5,
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildQuote(item),
            if (item.note?.text != null && item.note!.text!.isNotEmpty)
              Padding(
                padding: const EdgeInsets.only(top: 10),
                child: ContentCardText(item.note!.text!),
              ),
            ImageVideoGrid(
              padding: const EdgeInsets.only(top: 12),
              imageLinkList: item.note?.image ?? [],
              videoLink: item.note?.videoCover,
            ),
            if (item.note?.audio != null && item.note!.audio!.isNotEmpty)
              Padding(
                padding: const EdgeInsets.only(top: 12),
                child: AudioWaveWidget(
                  duration: item.note!.audioDuration ?? 0,
                ),
              ),
            IconNumRow(
              giveFlag: item.note?.giveFlag ?? 0,
              giveNum: item.note?.giveNum ?? 0,
              collectionFlag: item.note?.collectionFlag ?? 0,
              collectionNum: item.note?.collectionNum ?? 0,
              visitNum: item.note?.visitNum ?? 0,
              commentNum: item.note?.commentNum ?? 0,
              shareNum: item.note?.shareNum ?? 0,
              onLikeTap: (isGive) => controller.onGiveHandle(
                isGive,
                item.note?.noteId,
              ),
              onCollectionTap: (isGive) => controller.onCollectionHandle(
                isGive,
                item.note?.noteId,
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 引用
  Widget _buildQuote(MyCommentResponse item) {
    return Container(
      decoration: const BoxDecoration(
        border: Border(
          left: BorderSide(width: 4, color: Color(0xffefefef)),
        ),
      ),
      child: Padding(
        padding: const EdgeInsets.only(left: 10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            if (item.text != null && item.text!.isNotEmpty)
              _buildQuoteText(item.text!),
            if (item.image != null && item.image!.isNotEmpty)
              Padding(
                padding: const EdgeInsets.only(top: 6),
                child: SizedBox(
                  width: 50,
                  height: 50,
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(6),
                    child: CustomImage(url: item.image!),
                  ),
                ),
              ),
            if (item.audio != null && item.audio!.isNotEmpty)
              Padding(
                padding: const EdgeInsets.only(top: 6),
                child: AudioWaveWidget(
                  duration: item.audioDuration ?? 0,
                ),
              ),
          ],
        ),
      ),
    );
  }

  Widget _buildQuoteText(String text) {
    return Text.rich(TextSpan(
      text: '【他的评论】',
      style: const TextStyle(
        color: AppColors.danger,
        fontSize: 14,
      ),
      children: [
        TextSpan(
          text: text,
          style: const TextStyle(
            color: AppColors.active,
            fontSize: 14,
          ),
        ),
      ],
    ));
  }
}
